#test.py
#Daniel Lovell (edited by Timothy Sassone)
#Contains objects and methods which use the dice roller
#to perform various ingame tests.

import dice
import math

class test(object):
    def roll(self, total):
        """total dice"""
        dt = dice.thrower()
        throws, hits, glit, crit = dt.throw_ex(total)
        print(throws)
        print("Hits: " + str(hits))
        if glit:
            if crit: print ("Critical Glitch")
            else: print ("Glitch")
    def success(self, total, trsh):
        """total dice, threshold"""
        if total < trsh: print("Too few dice.")
        else:
            dt = dice.thrower()
            throws, hits, glit, crit = dt.throw_ex(total)
            print(throws)
            print("Hits: " + str(hits))
            if glit:
                if crit: print ("Critical Glitch")
                else: print ("Glitch")
            if hits >= trsh+4: print("Critical Success")
            elif hits >= trsh: print("Success")
            else: print ("Failure")
        return None

    def opposed(self, act, opp, grp_add=0): #Opposed Test
        opp = opp + grp_add
        
        dt = dice.thrower()
        athrows, ahits, aglit, acrit = dt.throw_ex(act)
        
        othrows, ohits, oglit, ocrit = dt.throw_ex(opp)
        
        print("Acting throws: \n" + str(athrows))
        print("Acting Hits: " + str(ahits))
        if aglit:
            if acrit: print ("Acting Critical Glitch")
            else: print ("Acting Glitch")
            
        print ("Opposing throws: \n" + str(othrows))
        print ("Opposing Hits: " + str(ohits))
        if oglit:
            if ocrit: print ("Opposing Critical Glitch")
            else: print ("Opposing Glitch")
            
        if ahits > ohits+4: print ("Critical Success")
        elif ahits > ohits: print ("Success")
        else: print ("Failure")
        return None

    def quick_etest(self, total, trsh, intl, intt="minutes"):
        """Dice Pool, threshold, interval length, interval type"""
        
        dt = dice.thrower()
        throws, hits, glit, crit = dt.throw_ex(total)
        
        print (throws)
        if glit:
            if crit: print("Critical Glitch Test Failed")
            else:
                print ("Glitch" )
                gthrows = dt.throw(1) # Rolls 1D6
                hits = hits - gthrow[0] #Subtracts value of 1D6 throw from hits
        if hits == 0: print ("No Hits: Test Failed") #No more hits after glitch
        else:
            avg = int(math.ceil(trsh/hits)) #Average number of test to
            if avg == 1: avg+= 1
            time = avg*intl #Total time required to meet threshold
            print("Average: " + str(avg))
            print("Test Succeds in " + str(time) + " " + intt)
        return None

    def single_etest(self, total, trsh, intl, intt="minutes",
                     lim=1000000):
        tot = 0
        tests = 0
        while total > 0:
            tests += 1
            time = tests*intl #Total time
            if time > lim:
                print ("Out of Time")
                break
            dt = dice.thrower()
            throws, hits, glit, crit = dt.throw_ex(total)
            print(throws)
            tot = tot + hits
            if glit:
                if crit:
                    print ("Critical Glitch: Test Failed")
                    fail = False
                    break
                else:
                    print ("Glitch")
                    gthrows = dt.throw(1) # Rolls 1D6
                    tot = tot - gthrow[0] #Subtracts value of 1D6 throw from hits
                    if tot <= 0: #No more hits after glitch
                         print ("No Hits After Glitch: Test Failed")
                         fail = False
                         break
            if tot >= trsh:
                print ("Test Succeds in " + str(time) + " " + intt)
                fail = False
                break
            total -= 1
            fail = True
        if fail: print ("Test Failed: Threshold Not Met In Time")
        return None

    def group_test(self, mod_lim, sec_dice):
        """maximum modifier, secondary character dicepools"""
        dt = dice.thrower()
        sec_num = len(sec_dice)
        pool_mod = 0
        threshold_mod = 0
        x = 0
        for x in range(0,sec_num):
            throws, hits, glit, crit = dt.throw_ex(sec_dice[x])
            pool_mod =  pool_mod + hits
            if crit: threshold_mod +=1
        if pool_mod >= mod_lim:
            pool_mod = mod_lim
        return pool_mod,threshold_mod
